<?php
/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/6/21
 * Time: 14:12
 */
class Kdtsalesman_model extends Base_Model
{
    private $_redis;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function checkExsit($table,$ret,$where=array())
    {
        $res = $this->db->select($ret)->from($table)->where($where)->get()->row_array();
        if (empty($res)) return false;
        else return $res;
    }

    public function insertData($table,$data)
    {
        $this->db->insert($table,$data);
        return $this->db->insert_id();
    }

    public function updateData($table,$data,$where){
        $this->db->update($table,$data,$where);
    }

    public function syncSalesman($data)
    {
        $exist = $this->checkExsit('mod_salesman','id',array('idcode'=>$data['idcode']));
        if ($exist===false){
            return $this->insertData('mod_salesman',$data);
        }else{return $exist['id'];}
    }

    public function synCoupon($data)
    {
        foreach ($data as $c){
            $row=array();
//            $row['couponid']=(int)$c['group_id'];
            $row['couponid']=(int)$c['id'];/*优惠的ID*/

//            $row['coupon_type']=$c['coupon_type'];/*优惠的类型。可选值：PROMOCARD（优惠券）、PROMOCODE（优惠码）*/
            $row['coupon_type']="PROMOCARD";
            $row['date_type']=(int)$c['date_type'];/*优惠使用时间类型，1表示固定活动时间，2表示延迟类型，几天后几天内有效*/

            $row['range_type']=$c['range_type'];/*PART（部分商品可用，或指定商品可用）ALL（全部商品可用）*/
            $row['title']=$c['title'];/*优惠标题*/

//            $row['is_random']=(int)$c['is_random'];/*是否是随机优惠，0 表示不随机，1 表示随机*/
            $row['is_random']=$c['value_random_to']>0?1:0;
//            $row['value_random_to']=(int)((float)$c['value_random_to']*100);/*如果 is_random 为 1，则该字段表示随机金额的上限。单位：元，精确到分*/
            $row['value_random_to']=(int)$c['value_random_to'];/*面值随机上限。不随机为0*/

//            $row['value']=(int)((float)$c['value']*100);/*优惠面额。如果 is_random 为 1，则该字段表示随机金额的下限。单位：元，精确到分*/
            $row['value']=(int)$c['denominations'];/*优惠面额，单位：分*/


//            $row['need_user_level']=(int)$c['need_user_level'];/*是否限制领用者的等级，0 表示不限制，大于 0 表示领用者必须是这个等级ID*/
//            $row['user_level_name']=$c['user_level_name'];/*限制领用者等级的名称*/
            $row['need_user_level']=(int)$c['user_level'];/*是否限制领用者的等级，0 表示不限制，大于 0 表示领用者必须是这个等级ID*/

//            $row['quota']=(int)$c['quota'];/*每人限领个数，为 0 则表示不限制*/
            $row['quota']=(int)$c['is_limit'];

//            $row['is_at_least']=(int)$c['is_at_least'];/*是否设置满多少元可用，0 表示不限制，1 表示限制*/
//            $row['at_least']=(int)((float)$c['at_least']*100);/*如果 is_at_least 为 1，该字段表示订单必须满这个价格，优惠才可用。单位：元，精确到分*/
            $row['at_least']=(int)$c['condition'];/*满额条件，单位：分*/

//            $row['total']=(int)$c['total'];/*总库存*/
            $row['total']=(int)$c['total_qty'];
//            $row['stock']=(int)$c['stock'];/*剩余可用库存*/
            $row['stock']=(int)$c['stock_qty'];

            $row['fixed_term']=(int)$c['fixed_term'];/*固定时长*/
            $row['fixed_begin_term']=(int)$c['fixed_begin_term'];/*延迟多长时间开始*/

//            $row['start']=strtotime($c['start_at']);/*优惠生效时间，时间格式：yyyy-MM-dd HH:mm:ss*/
            $row['start']=strtotime($c['valid_start_time']);

//            $row['end']=strtotime($c['end_at']);/*优惠结束时间，时间格式：yyyy-MM-dd HH:mm:ss*/
            $row['end']=strtotime($c['valid_end_time']);

//            $row['expire_notice']='';/*是否到期前4天发送提醒，0 表示不发送，1 表示发送*/
            $row['description']=$c['description'];/*使用说明*/
            $row['is_forbid_preference']=(int)$c['is_forbid_preference'];/*是否仅原价购买商品时可用，0 表示否，1 表示是*/
//            $row['is_sync_weixin']='';/*是否同步微信卡券，0 表示否，1 表示是*/
//            $row['weixin_card_id']='';/*微信卡券ID*/
//            $row['status']=(int)$c['status'];/*优惠状态，0 表示有效，1 表示失效，2 表示微信卡券审核中*/
            $row['status']=(int)$c['is_invalid'];

            $row['is_share']=(int)$c['is_share'];/*是否可分享领取链接，0 表示否，1 表示是*/
            $row['fetch_url']=$c['url'];/*优惠领取链接*/
//            $row['stat_fetch_user_num']='';/*领取优惠的人数*/
            $row['total_fans_taked']=(int)$c['total_fans_taked'];
//            $row['stat_fetch_num']='';/*领取优惠的次数*/
            $row['total_take']=(int)$c['total_take'];
//            $row['stat_use_num']='';/*使用优惠的次数*/
            $row['total_used']=(int)$c['total_used'];

//            $row['created']=strtotime($c['created']);/*优惠创建时间，时间格式：yyyy-MM-dd HH:mm:ss*/
            $row['created']=strtotime($c['created_at']);

            $this->db->reconnect();
            $exist = $this->checkExsit('kdt_coupons','id',array('couponid'=>$row['couponid']));
            if ($exist===false){
                $this->insertData('kdt_coupons',$row);
            }else{
                $this->updateData('kdt_coupons',$row,array('id'=>$exist['id']));
            }
            $this->db->close();
        }
        $this->db->reconnect();
        $this->db->delete('kdt_coupons',array('end <'=>time()));//删除过期优惠券
        $this->db->delete('kdt_coupons',array('status'=>1));//删除失效优惠券
        $this->db->close();
    }

    public function getAllCoupons()
    {
        return $this->db->select('couponid,title')->from('kdt_coupons')->get()->result_array();
    }

    public function getSlsType()
    {
        $res = $this->db->select('*')->from('mod_salesman_type')->get()->result_array();
        $ret = array();
        foreach ($res as $row)
        {
            $ret[$row['type']]=$row['name'];
        }
        return $ret;
    }

    public function addBatchCoupons($coupontype,$coupons)
    {
        //获取渠道销售员
        $this->db->select('idcode')->from('mod_salesman');
        if ($coupontype!=='ALL') $this->db->where(array('type'=>$coupontype));
        $sms=$this->db->get()->result_array();

        if (!empty($sms)){
            $this->load->model('redis_model');
            $redis=$this->redis_model->getRedisInstance();
            //给每个销售员发券
            foreach ($sms as $s){
                $redis->multi();
                foreach($coupons as $c){
                    $redis->sAdd('seller:'.$s['idcode'].'.coupons',$c);
                }
                $redis->exec();
            }
        }
    }

    public function getEditCoupons($id)
    {
        $sls = $this->db->select('idcode')->from('mod_salesman')->where(array('id'=>$id))->get()->row_array();
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        $coupons=$redis->sMembers('seller:'.$sls['idcode'].'.coupons');
        $all=$this->getAllCoupons();
        $ret = array();
        foreach ($all as $c) {
            $retrow=array();
            $retrow['title']=$c['title'];
            $retrow['couponid']=$c['couponid'];
            if (in_array($c['couponid'],$coupons)) $retrow['checked']=1;
            else $retrow['checked']=0;
            $ret[]=$retrow;
        }
        return $ret;
    }

    public function editCoupons($id,$coupons)
    {
        $sls = $this->db->select('idcode')->from('mod_salesman')->where(array('id'=>$id))->get()->row_array();
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();
        $redis->delete('seller:'.$sls['idcode'].'.coupons');
        if (!empty($coupons)){
            $redis->multi();
            foreach($coupons as $c){
                $redis->sAdd('seller:'.$sls['idcode'].'.coupons',$c);
            }
            $redis->exec();
        }
    }

    public function getSalesmanArticleData()
    {
        $sms = $this->db->select('name,phone,idcode')->from('mod_salesman')->get()->result_array();
        $this->load->model('redis_model');
        $redis=$this->redis_model->getRedisInstance();

        $data=array();
        foreach ($sms as $sm){
            $models=$redis->sMembers('salesman.'.$sm['idcode'].'.article');
            if(!empty($models)){
                $articlenum=count($models);

                $artread=0;
                foreach ($models as $m){
                    $artid=$redis->get('salesman.article.reverse.'.$m.$sm['idcode']);
                    $artread1=$redis->bitCount('salesman.article.read.'.$artid);
                    $artread+=(int)$artread1;
                }

                $artprofit=$artread*0.1;//文章总收益

                $row=array('name'=>$sm['name'],'phone'=>$sm['phone'],'num'=>$articlenum,'read'=>$artread,'profit'=>$artprofit);
                $data[]=$row;
                echo $row['name'].' share:'.$row['num'].' read:'.$row['read'].' profit:'.$row['profit'].PHP_EOL;
            }
//            else{$row=array('name'=>$sm['name'],'phone'=>$sm['phone'],'num'=>0,'read'=>0,'profit'=>0);}

        }
        array_unshift($data,array('姓名','电话','分享文章','文章阅读人数','收益'));
        return $data;
    }

    public function getSalesmanPostListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->select('id,type,name,pid,url,status,created,sort')->from('mod_salesman_post_v2');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('sort ASC,created DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['sort'];
            $resrow[] = $row['name'];
            $resrow[] = $this->getSlspostType($row['type']);
            $resrow[] = $this->getSlspostTarget($row['pid']);
            $resrow[] = '<img src="http://wap.nldou.com'.$row['url'].'" style="height:30px;">';
            $resrow[] = $row['status']==1?"展现":"隐藏";
            $resrow[] = $this->getSlspostScanUser($row['id']);
            $resrow[] = $this->getSlspostScanTimes($row['id']);
            $resrow[] = date('Y-m-d H:i:s',$row['created']);
            $actionlist ='<a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">修改</a>';
            $actionlist .= '<a class="other-btn action-btn" href="#" data-pkey="'.$row['id'].'" data-sort="'.$row['sort'].'">排序</a>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSlspostTarget($pid)
    {
        $res = $this->db->select('name')->from('mod_slspost_route')->where('pid =',$pid)->get()->row_array();
        return $res['name'];
    }
    public function getSlspostType($type)
    {
        $res = $this->db->select('name')->from('mod_slspost_type')->where('id =',$type)->get()->row_array();
        return $res['name'];
    }

    public function getSlspostScanUser($postid)
    {
        if (!isset($this->_redis)){
            $this->load->model('redis_model');
            $this->_redis = $this->redis_model->getRedisInstance();
        }
        return $this->_redis->bitCount('slspostv2.global.postscanbit.'.$postid);
    }

    public function getSlspostScanTimes($postid)
    {
        $res = $this->db->select('count(*) as times')->from('mod_slspost_stat')->where('postid =',$postid)->get()->row_array();
        return $res['times'];
    }

    public function getSalesmanProductListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->from('mod_salesman_goods');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('sort ASC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['num_iid'];
            $resrow[] = '<img src="http://wap.nldou.com'.$row['cover'].'" style="height:30px;">';
            $resrow[] = $row['title'];
            $resrow[] = (float)$row['price']/100;
            $resrow[] = (float)$row['priceUnderLine']/100;
            $resrow[] = (float)$row['i_rate']/100;
            $resrow[] = $row['status']==1?"显示":"隐藏";
            $actionlist ='<a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">修改</a>';
            $actionlist .= '<a class="sort-btn other-btn action-btn" href="#" data-pkey="'.$row['id'].'" data-sort="'.$row['sort'].'">排序</a>';
            $actionlist .= '<a class="other-btn action-btn" href="'.site_url('admin/salesman/salesmanproductimgs/').'showlist/'.$row['id'].'/1">添加图片</a>';
            $actionlist .= '<a class="other-btn action-btn" href="'.site_url('admin/salesman/salesmanproducttext/').'showlist/'.$row['id'].'/1">添加文案</a>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSalesmanInviPostListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->from('mod_salesman_invipost');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('sort ASC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['sort'];
            $resrow[] = '<img src="http://wap.nldou.com'.$row['url'].'" style="height:30px;">';
            $resrow[] = $row['name'];
            $resrow[] = $row['status']==1?"显示":"隐藏";
            $actionlist ='<a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">修改</a>';
            $actionlist .= '<a class="sort-btn other-btn action-btn" href="#" data-pkey="'.$row['id'].'" data-sort="'.$row['sort'].'">排序</a>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function sortEdit($id,$sort,$table)
    {
        $res = $this->db->select('sort')->from($table)->where('id =',$id)->get()->row_array();
        $old = (int)$res['sort'];
        //更新相关项序号
        if ($old<$sort){
            $this->db->set('sort','sort-1',false);
            $this->db->where('sort >',$old);
            $this->db->where('sort <=',$sort);
            if (!$this->db->update($table))
                return false;
        }elseif($old>$sort) {
            $this->db->set('sort','sort+1',false);
            $this->db->where('sort >=',$sort);
            $this->db->where('sort <',$old);
            if (!$this->db->update($table))
                return false;
        }else{
            return true;
        }
        //更新序号
        return $this->db->update($table,array('sort'=>$sort),array('id'=>$id));
    }
    public function createSortEdit($sort,$table)
    {
        //更新相关项序号
        $this->db->set('sort','sort+1',false);
        $this->db->where('sort =',$sort);
        $this->db->or_where('sort >',$sort);
        $this->db->update($table);
    }

    public function getGoodsnameByGid($gid)
    {
        $res = $this->db->select('title')->from('mod_salesman_goods')->where('id =',$gid)->get()->row_array();
        return $res['title'];
    }

    public function text2br($text)
    {
        $text=trim($text);
        return str_replace("\r","<br/>",str_replace("\n","<br/>",str_replace("\r\n","<br/>",$text)));
    }

    public function getPrevProcessedMoney($sls,$created)
    {
        $res=$this->db->select('SUM(money) as procMoney')->from('mod_salesman_money')
            ->where(array('sls ='=>$sls,'status ='=>2,'created <'=>$created))->get()->row_array();
        return (int)$res['procMoney'];
    }

    public function getSlsmoneyListData($controller,$where=array(),$limit=10,$offset=0)
    {
        if (!isset($this->_redis)){
            $this->load->model('redis_model');
            $this->_redis = $this->redis_model->getRedisInstance();
        }
        $this->db->select('m.id,m.tid,m.sls,s.name,u.userName,s.phone,m.money,m.created,m.process,m.status')->from('mod_salesman_money as m')
            ->join('mod_salesman as s','m.sls=s.idcode','left')
            ->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('m.created DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['tid'];
            $resrow[] = $row['name'];
            $resrow[] = $row['userName'];
            $resrow[] = $row['phone'];

            $this->_redis->multi();
            $this->_redis->get('slspostv2.scantotal.'.$row['sls']);
            $this->_redis->get('slsartv2.readtotal.'.$row['sls']);
            $moneyres = $this->_redis->exec();
            $totalmoney = ((int)$moneyres[0]+(int)$moneyres[1])*0.1;
            $resrow[] = $totalmoney;

            $resrow[] = $this->getPrevProcessedMoney($row['sls'],$row['created'])/100;

            $resrow[] = (float)$row['money']/100;
            $resrow[] = date('Y-m-d H:i:s',$row['created']);
            $resrow[] = $row['process']==0?'未处理':date('Y-m-d H:i:s',$row['process']);
            $resrow[] = $row['status']==1?'未处理':'已处理';
            $actionlist ='<div style="min-width:50px;"><a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">处理</a></div>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSalesmanArticleListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->select('id,sort,title,model,cover,created')->from('mod_salesman_article');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('sort ASC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['sort'];
            $resrow[] = $row['title'];
            $resrow[] = $row['model'];
            $resrow[] = '<img src="http://wap.nldou.com'.$row['cover'].'" style="height:30px;">';
            $resrow[] = $this->getSlsartRead($row['model']);
            $resrow[] = date('Y-m-d',$row['created']);
            $actionlist ='<a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">修改</a>';
            $actionlist .= '<a class="sort-btn other-btn action-btn" href="#" data-pkey="'.$row['id'].'" data-sort="'.$row['sort'].'">排序</a>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSlsartRead($model)
    {
        if (!isset($this->_redis)){
            $this->load->model('redis_model');
            $this->_redis = $this->redis_model->getRedisInstance();
        }
        return $this->_redis->bitCount('slsartv2.global.readtotalbit.'.$model);
    }

    public function getTotalDemandMoney($sls)
    {
        $res=$this->db->select('SUM(money) as totalDemandMoney')->from('mod_salesman_money')
            ->where('sls =',$sls)->get()->row_array();
        return (int)$res['totalDemandMoney'];
    }

    public function getSalesmanListData($controller,$where=array(),$limit=10,$offset=0)
    {
        if (!isset($this->_redis)){
            $this->load->model('redis_model');
            $this->_redis = $this->redis_model->getRedisInstance();
        }
        $this->db->select('s.id,s.name,u.userName,s.idcode,s.phone,s.invifrom,s.type,s.created')
            ->from('mod_salesman as s')->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('s.created DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            $resrow[] = $row['name'];
            $resrow[] = $row['userName'];
            $resrow[] = $row['idcode'];
            $resrow[] = $row['phone'];

            if (!empty($row['invifrom'])){
                $invitor = $this->db->select('name')->from('mod_salesman')->where('phone =',$row['invifrom'])->get()->row_array();
                $resrow[]=empty($invitor['name'])?$row['invifrom']:$invitor['name'];
            }else{$resrow[]='';}

            $typeres = $this->db->select('name')->from('mod_salesman_type')->where('type',$row['type'])->get()->row_array();
            $resrow[] = $typeres['name'];

            $this->_redis->multi();
            $this->_redis->get('slspostv2.scantotal.'.$row['idcode']);
            $this->_redis->get('slsartv2.readtotal.'.$row['idcode']);
            $moneyres = $this->_redis->exec();
            $totalmoney = ((int)$moneyres[0]+(int)$moneyres[1])*0.1;
            $resrow[] = $totalmoney;

            $demandmoney = $this->getTotalDemandMoney($row['idcode']);
            $demandmoney /= 100;
            $resrow[] = $demandmoney;

            $resrow[] = date('Y-m-d H:i:s',$row['created']);
            $actionlist ='<a class="eidt-btn action-btn" href="'.site_url($controller).'update/'.$row['id'].'">修改</a>';
            $actionlist .= '<a class="other-btn action-btn" href="#" data-pkey="'.$row['id'].'">优惠券</a>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSalesmanmanageListData($controller,$where=array(),$limit=10,$offset=0)
    {
        if (!isset($this->_redis)){
            $this->load->model('redis_model');
            $this->_redis = $this->redis_model->getRedisInstance();
        }
        $this->db->select('s.id,s.name,u.userName,s.idcode,s.phone,s.invifrom,s.type,s.created')
            ->from('mod_salesman as s')->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($where)) $this->db->where($where);
        $data=$this->db->order_by('s.created DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            //销售员名称
            $resrow[] = $row['name'];
            //微信昵称
            $resrow[] = $row['userName'];
            //销售员ID
            $resrow[] = $row['idcode'];
            //电话
            $resrow[] = $row['phone'];
            //上线
//            if (!empty($row['invifrom'])){
//                $invitor = $this->db->select('name')->from('mod_salesman')->where('phone =',$row['invifrom'])->get()->row_array();
//                $resrow[]=empty($invitor['name'])?$row['invifrom']:$invitor['name'];
//            }else{$resrow[]='';}
            //渠道
//            $typeres = $this->db->select('name')->from('mod_salesman_type')->where('type',$row['type'])->get()->row_array();
//            $resrow[] = $typeres['name'];
            //累积下线
            $resrow[] = $this->db->from('mod_salesman')->where('invifrom',$row['phone'])->count_all_results();
            //文章触达人数
            $artres   = $this->db->select('uid')->from('mod_slsarticle_stat')->where('sls',$row['idcode'])->group_by('uid')->get()->result_array();
            $resrow[] = count($artres)."人";
            //海报触达人数
            $postres  = $this->db->select('uid')->from('mod_slspost_stat')->where('sls',$row['idcode'])->group_by('uid')->get()->result_array();
            $resrow[] = count($postres)."人";
            //有效文章
            $resrow[] = $this->_redis->sCard('salesman.'.$row['idcode'].'.article')."篇";
            //有效海报
            $resrow[] = $this->_redis->sCard('slspostv2.posts.'.$row['idcode'])."张";
            //加入日期
            $resrow[] = date('Y-m-d H:i:s',$row['created']);
            //操作
            $actionlist = '<div class="flex-row flex-align-center flex-justify-around" style="min-width: 80px;"><a class="other-btn action-btn" href="'.site_url("admin/salesman/salesmanartvisitor/").'showlist/'.$row['idcode'].'/1">文章</a>';
            $actionlist .= '<a class="other-btn action-btn" href="'.site_url("admin/salesman/salesmanpostscanner/").'showlist/'.$row['idcode'].'/1">海报</a></div>';
            $resrow[] = $actionlist;
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSalesmanartvisitorListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->select("u.userIcon,u.userName,COUNT(s.readtimes) as artnum")
            ->from('mod_slsarticle_stat as s')
            ->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($where)) $this->db->where($where);
        $this->db->group_by("s.uid");
        $data=$this->db->order_by('artnum DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            //头像
            $img = strpos($row['userIcon'],'http')===0?$row['userIcon']:"http://wap.nldou.com".$row['userIcon'];
            $resrow[] = '<img src="'.$img.'" style="width:30px;">';
            //微信昵称
            $resrow[] = $row['userName'];
            //阅读次数
            $resrow[] = $row['artnum'];
            $res[] = $resrow;
        }
        return $res;
    }

    public function getSalesmanpostscannerListData($controller,$where=array(),$limit=10,$offset=0)
    {
        $this->db->select("u.userIcon,u.userName,s.uid,COUNT(s.scantimes) as scannum")
            ->from('mod_slspost_stat as s')
            ->join('mod_user as u','u.uid=s.uid','left');
        if (!empty($where)) $this->db->where($where);
        $this->db->group_by("s.uid");
        $data=$this->db->order_by('scannum DESC')->limit($limit,$offset)
            ->get()->result_array();
        $res = array();
        foreach ($data as $row){
            $resrow = array();
            //头像
            $img = strpos($row['userIcon'],'http')===0?$row['userIcon']:"http://wap.nldou.com".$row['userIcon'];
            $resrow[] = '<img src="'.$img.'" style="width:30px;">';
            //微信昵称
            $resrow[] = $row['userName'];
            //扫描海报
            $postres  = $this->db->select('postid')->from('mod_slspost_stat')->where(array('sls'=>$where['s.sls'],'uid'=>$row['uid']))->group_by('postid')->get()->result_array();
            $resrow[] = count($postres);
            //扫描次数
            $resrow[] = $row['scannum'];
            $res[] = $resrow;
        }
        return $res;
    }
}